<html>

<head>
	<script>

		function sendMessage(params) {
			return new Promise((resolve, reject) => {
				const evt = document.createEvent("CustomEvent");
				const listener = (res) => {
					window.document.body.removeEventListener('koimy-ic-e002', listener);
					const obj = res.detail;
					resolve(obj);
				};
				window.document.body.addEventListener('koimy-ic-e002', listener);
				evt.initCustomEvent('koimy-ic-e001', true, false, params);
				document.dispatchEvent(evt);
			});
		}

		function startApp() {

			const service_name = document.getElementById('service_name').value
			const params = document.getElementById('params').value
			const returns = document.getElementById('returns')
			sendMessage({
				service_name,
				params: {
					...JSON.parse(params)
				}
			}).then((res) => {
				returns.value = JSON.stringify(res)
				console.log(res)
			}).catch((e) => {
				console.error(e)
			});
		}

	</script>
</head>

<body>
	<input id="service_name" placeholder="请输入要调用的服务名" type="text" style="width:300px;" value="dc_init_w">
	<button type="button" onClick="startApp()" id="startApp">测试</button>
	<br>
	<textarea id="params" cols="150" rows="10" placeholder="JSON格式数据">{"port":0,"baud":9600}</textarea>
	<textarea id="returns" cols="150" rows="10" placeholder="返回值"></textarea>
</body>

</html>